//
//  LocalMusicViewController.m
//  wAudioShare
//
//  Created by 赵帅 on 14-1-14.
//  Copyright (c) 2014年 赵帅. All rights reserved.
//

#import "LocalMusicViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import "MMProgressHUD.h"

@interface LocalMusicViewController ()
<servicePlayListDemoDelegate,serviceSetPlayModeDelegate>
{
    NSMutableArray * localMusicArray;
    MPVolumeView *volumeView;
}

@end

@implementation LocalMusicViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //wav mp3 wma ape flac
    localMusicArray = [NSMutableArray array];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error = nil;
    NSArray *fileList = [[NSArray alloc] init];
    //fileList便是包含有该文件夹下所有文件的文件名及文件夹名的数组
    
    NSString *homeDir = NSHomeDirectory();
    NSString * kDocumentsPath = [homeDir stringByAppendingPathComponent:@"Documents"];
    
    fileList = [fileManager contentsOfDirectoryAtPath:kDocumentsPath error:&error];
    
    for(NSString * name in fileList)
    {
        if([[name pathExtension] isEqualToString:@"mp3"] || [[name pathExtension] isEqualToString:@"wav"] || [[name pathExtension] isEqualToString:@"wma"] ||[[name pathExtension] isEqualToString:@"ape"] || [[name pathExtension] isEqualToString:@"flac"])
        {
            [localMusicArray addObject:name];
        }
    }
    
//    volumeView = [[MPVolumeView alloc] initWithFrame: CGRectMake(100, 100, 100, 100)];
//    volumeView.backgroundColor = [UIColor blackColor];
//    volumeView.showsVolumeSlider = NO;
//    volumeView.showsRouteButton = YES;
//    [self.view addSubview: volumeView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navigation methods
-(NSString *)navigationBarTitle
{
    return NSLocalizedString(@"LOCAL_MUSIC",@"");;
}

-(BOOL)isNewVersionButtonEnabled
{
    return YES;
}

#pragma mark -
#pragma mark UITableView Delegate & Datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [localMusicArray count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }
    else
    {
        for(UIView * subview in [cell.contentView subviews])
        {
            [subview removeFromSuperview];
        }
    }
    
    // Configure the cell
    cell.textLabel.text = localMusicArray[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
    
    [[GlobalService sharedInstance] setPlayMode:self playMode:sequencemode];
    
    //制作m3u文件
    NSMutableString * m3uStr = [NSMutableString string];
    NSString *homeDir = NSHomeDirectory();
    NSString * kDocumentsPath = [homeDir stringByAppendingPathComponent:@"Documents"];
    
    for(int i=indexPath.row;i<[localMusicArray count];i++)
    {
        [m3uStr appendFormat:@"http://%@:%d/%@\n",[GlobalService sharedInstance].localIP,[GlobalService sharedInstance].localPort,localMusicArray[i]];
    }
    
    NSData *data = [m3uStr dataUsingEncoding:NSUTF8StringEncoding];
    NSString *filename = [NSString stringWithFormat:@"%@/songs.m3u",kDocumentsPath];
    [data writeToFile: filename atomically: NO];

    [[GlobalService sharedInstance] playListDemo:self musicURL:[NSString stringWithFormat:@"http://%@:%d/songs.m3u",[GlobalService sharedInstance].localIP,[GlobalService sharedInstance].localPort] masterIP:nil groupName:nil];
}

-(void)servicePlayListDemoCallBack:(NSDictionary*)dicCallBack
{
    if([[dicCallBack objectForKey:@"responsestatus"] intValue] == [SERVICE_SUCCESS intValue])
    {
        [MMProgressHUD dismiss];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"JumptoControlView" object:nil];
    }
    else
    {
        [MMProgressHUD dismissWithError:NSLocalizedString(@"MUSIC_PLAY_FAILED",@"")];
    }
}

-(void)serviceSetPlayModeCallBack:(NSDictionary*)dicCallBack{}

@end
